// pages/mch/car/user.js
const App = getApp()
import Util from '../../../utils/util'

Page({
  data: {
    init: false,

    tab: '1',
    tabIndex: 0,
    CustomBar: App.Global.CustomBar,

    type: 2,
    amt: true,
    rate: true,

    clks: [],

    query: false,
    mob: '',
    mem: {
      id: '',
      name: '',
      mob: ''
    },

    sel_uid: '',
    sel_name: '',
    sel_type: 1, // 1 金额，2 比例
    sel_amt: 0,
    sel_rate: 0,

    mem_mob: '',
    mem_name: '',
    mem_idno: '',
  },
  onLoad(options) {
    this.execLoad(options)
  },
  execLoad(options) {
    this.setData({
      loading: true
    })
    App.doPost({
      uri: '/app/merchant/clerk/list',
      body: {

      },
      fnSucc: resp => {
        this.setData({
          init: true,
          type: parseInt(options.type),
          amt: options.amt != 'false',
          rate: options.rate != 'false',
          sel_type: options.amt != 'false' ? 1 : 2,
          clks: resp.data.items
        })
      }
    })
  },

  // 查询会员
  execQuery() {
    this.setData({
      query: false
    })
    if (!this.data.mob) {
      Util.Notify.warn(`请填写手机号`)
      return
    }
    App.doPost({
      uri: '/comm/biz/member/query',
      body: {
        mob: this.data.mob
      },
      fnSucc: resp => {
        this.setData({
          query: true,
          mem: resp.data,
          mem_mob: this.data.mob
        })
      }
    })
  },
  doSubmitSelect() {
    if (this.data.sel_type == 1 && this.data.sel_amt <= 0) {
      Util.Notify.warn(`请填写正确的分润金额`)
      return
    }
    if (this.data.sel_type == 2 && (this.data.sel_rate <= 0 || this.data.sel_rate > 100)) {
      Util.Notify.warn(`请填写正确的分润比例`)
      return
    }
    const event = this.getOpenerEventChannel()
    event.emit('enter', {
      uid: this.data.sel_uid,
      name: this.data.sel_name,
      type: this.data.sel_type,
      amt: this.data.sel_amt,
      rate: this.data.sel_rate
    })
    wx.navigateBack({
      delta: 1,
    })
  },
  doEnterSelect() {
    if (this.data.tab == '2') {
      if (this.data.mem.id) {
        this.setData({
          sel_uid: this.data.mem.id,
          sel_name: this.data.mem.name
        })
      } else {

        if (!this.data.mem_idno || !this.data.mem_mob || !this.data.mem_name) {
          Util.Notify.warn(`请填写店外人员信息`)
          return
        }

        App.doPost({
          uri: '/comm/biz/member/create',
          body: {
            mob: this.data.mem_mob,
            name: this.data.mem_name,
            idno: this.data.mem_idno,
          },
          fnSucc: resp => {
            this.setData({
              sel_uid: resp.data.id,
              sel_name: this.data.mem_name
            })
            this.doSubmitSelect()
          }
        })
        return
      }

    } else {
      if (!this.data.sel_uid) {
        Util.Notify.warn(`请选择人员`)
        return
      }
    }
    this.doSubmitSelect()
  },
  doEnterMch() {
    if (this.data.sel_type == 1 && this.data.sel_amt <= 0) {
      Util.Notify.warn(`请填写正确的分润金额`)
      return
    }
    if (this.data.sel_type == 2 && (this.data.sel_rate <= 0 || this.data.sel_rate > 100)) {
      Util.Notify.warn(`请填写正确的分润比例`)
      return
    }
    const event = this.getOpenerEventChannel()
    event.emit('enter', {
      uid: 'mchid',
      name: '门店账户',
      type: this.data.sel_type,
      amt: this.data.sel_amt,
      rate: this.data.sel_rate
    })
    wx.navigateBack({
      delta: 1,
    })
  },
  doEnterShare() {
    // if (this.data.sel_type == 1 && this.data.sel_amt <= 0) {
    //   Util.Notify.warn(`请填写正确的分润金额`)
    //   return
    // }
    // if (this.data.sel_type == 2 && (this.data.sel_rate <= 0 || this.data.sel_rate > 100)) {
    //   Util.Notify.warn(`请填写正确的分润比例`)
    //   return
    // }
    const event = this.getOpenerEventChannel()
    event.emit('enter', {
      uid: 'share',
      name: '获客人员',
      type: 2, // this.data.sel_type,
      amt: 0, //parseInt(this.data.sel_amt + ''),
      rate: 100 // this.data.sel_rate
    })
    wx.navigateBack({
      delta: 1,
    })
  },
  // 滑动切换
  doSwiperChange(e) {
    let target = e.detail.currentItemId
    if (this.data.tab === target) {
      return
    }
    this.setData({
      tab: target
    })
  },
  // 选择店内人员
  doSelectClerk(e) {
    let clk = e.currentTarget.dataset.clk
    this.setData({
      sel_uid: clk.id,
      sel_name: clk.name,
    })
  },
  // 切换类型
  doTypeChange(e) {
    this.setData({
      sel_type: parseInt(e.currentTarget.dataset.type)
    })
  },
  // 输入检查
  doCheckInput(e) {
    let value = parseFloat(e.detail.value || '0')
    let min = parseFloat(e.target.dataset.min || '0')
    let max = parseFloat(e.target.dataset.max || '99999')
    if (value < min) {
      return min + ''
    }
    if (value > max) {
      return max + ''
    }
    return parseFloat(value.toFixed(2)) + ''
  },
  toSwiper(e) {
    this.setData({
      tabIndex: parseInt(e.currentTarget.dataset.idx)
    })
  }
})